package com.iflytek.jobs.service.service.impl;

import cn.hutool.http.HttpUtil;
import com.iflytek.jobs.service.domain.JobDelivery;
import com.iflytek.jobs.service.domain.ResumeLog;
import com.iflytek.jobs.service.mapper.JobDeliveryMapper;
import com.iflytek.jobs.service.service.IJobDeliveryService;
import com.iflytek.jobs.service.service.IResumeLogService;
import com.iflytek.jobs.service.service.IResumeService;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 简历投递记录Service业务层处理
 * 
 * @author chenhs
 * @date 2023-04-26
 */
@Service
public class JobDeliveryServiceImpl implements IJobDeliveryService {
	@Autowired
	private JobDeliveryMapper jobDeliveryMapper;
	@Autowired
	private IResumeLogService resumeLogService;
	@Autowired
	private IResumeService resumeService;
	@Value("${server.servlet.context-path:}")
	private String contextPath;

	/**
	 * 查询简历投递记录
	 * 
	 * @param jdId 简历投递记录主键
	 * @return 简历投递记录
	 */
	@Override
	public JobDelivery selectJobDeliveryByJdId(Integer jdId) {
		return jobDeliveryMapper.selectJobDeliveryByJdId(jdId);
	}

	/**
	 * 查询简历投递记录列表
	 * 
	 * @param jobDelivery 简历投递记录
	 * @return 简历投递记录
	 */
	@Override
	public List<JobDelivery> selectJobDeliveryList(JobDelivery jobDelivery) {
		return jobDeliveryMapper.selectJobDeliveryList(jobDelivery);
	}

	/**
	 * 新增简历投递记录
	 * 
	 * @param jobDelivery 简历投递记录
	 * @return 结果
	 */
	@Override
	public int insertJobDelivery(JobDelivery jobDelivery) {
		// 如果没有简历指导记录，则不能投递
		ResumeLog rl = new ResumeLog();
		rl.setReId(jobDelivery.getReId());
		boolean hasLog = resumeLogService.selectResumeLogList(rl).isEmpty();
		if (hasLog) {
			return -1;
		}
		// 防止重复插入
		// JobDelivery jd = new JobDelivery();
		// jd.setJobId(jobDelivery.getJobId());
		// jd.setStuId(jobDelivery.getStuId());
		// boolean empty = selectJobDeliveryList(jd).isEmpty();
		// if (!empty) {
		// 	return -2;
		// }
		// 插入投递记录
		jobDelivery.setCreateTime(DateUtils.getNowDate());
		return jobDeliveryMapper.insertJobDelivery(jobDelivery);
	}

	/**
	 * 修改简历投递记录
	 * 
	 * @param jobDelivery 简历投递记录
	 * @return 结果
	 */
	public int updateJobDelivery(JobDelivery jobDelivery) {
		jobDelivery.setUpdateTime(DateUtils.getNowDate());
		return jobDeliveryMapper.updateJobDelivery(jobDelivery);
	}

	/**
	 * 删除简历投递记录信息
	 * 
	 * @param jdId 简历投递记录主键
	 * @return 结果
	 */
	public int deleteJobDeliveryByJdId(Integer jdId) {
		// 如果是没下载过的简历可以删除
		Integer downStatus = selectJobDeliveryByJdId(jdId).getDownStatus();
		if (downStatus == 1) {
			return -1;
		}
		return jobDeliveryMapper.deleteJobDeliveryByJdId(jdId);
	}

	public byte[] download(String baseUrl,Integer[] jdIds) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		for (Integer jdId : jdIds) {
			JobDelivery jd = this.selectJobDeliveryByJdId(jdId);
			String resumeUrl = resumeService.selectResumeByReId(jd.getReId()).getResumeUrl();
			String url = baseUrl + resumeUrl;
			byte[] bytes = HttpUtil.downloadBytes(url);
			try {
				zip.putNextEntry(new ZipEntry(jd.getJob().getCom() + "-" + jd.getJob().getJobName() + "-"
						+ jd.getStu().getScl().getSclName() + "-" + jd.getStu().getStuName() + ".pdf"));
				zip.write(bytes);
				zip.flush();
				zip.closeEntry();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	public void batchUpdateDownloadStatus(Integer[] jdIds) {
		// 批量更新投递状态
		for (Integer jdId : jdIds) {
			JobDelivery delivery = new JobDelivery();
			delivery.setJdId(jdId);
			delivery.setDownStatus(1);
			delivery.setUpdateTime(new Date());
			updateJobDelivery(delivery);
		}
	}
}
